/**
 * Copyright (c) 2022 Presto Labs Pte. Ltd.
 * Author: Ju
 */

#include "coin2/base/memory/mem_slice.h"

#include <sstream>

#include "coin2/base/memory/mem_buf.h"

namespace coin2::base::memory {

MemSlice MemSlice::Clone() const {
  MemSlice tmp(buffer_->Clone());

  tmp.begin_offset_ = begin_offset_;
  tmp.end_offset_ = end_offset_;

  return tmp;
}

MemBuf *MemSlice::CloneSlicedMem() const {
  return MemBuf::Create(Begin(), GetUsedSize());
}

std::string MemSlice::ToDebugString() const {
  std::ostringstream oss;
  auto sz = GetUsedSize();
  bool first = true;
  for (auto i = 0u; i < sz; ++i) {
    if (first) {
      first = false;
    } else {
      oss << " ";
    }
    oss << "0x" << std::hex
        << (static_cast<uint32_t>(
                *(static_cast<const uint8_t *>(Begin()) + i)) &
            0xff);
  }
  return oss.str();
}

}  // namespace coin2::base::memory